package com.easyCode.mysql;

import com.easyCode.mysql.constants.MysqlFiledTypeConstants;
import com.easyCode.mysql.sqlBuilder.sqlInfo.MysqlDataBaseSqlInfo;
import com.easyCode.mysql.sqlBuilder.sqlInfo.MysqlFiledSqlInfo;
import com.easyCode.mysql.sqlBuilder.sqlInfo.MysqlKeySqlInfo;
import com.easyCode.mysql.sqlBuilder.sqlInfo.MysqlTableSqlInfo;
import com.easyCode.sqlBuilder.sqlInfo.BaseDataBaseSqlInfo;
import com.easyCode.sqlBuilder.sqlInfo.BaseFiledSqlInfo;
import com.easyCode.sqlBuilder.sqlInfo.BaseKeySqlInfo;
import com.easyCode.sqlBuilder.sqlInfo.BaseTableSqlInfo;
import com.easyCode.sqlManager.MappingContainer;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.Date;

/**
 * @author 大忽悠
 * @create 2022/12/8 21:25
 */
public class MysqlMappingContainer extends MappingContainer {

    static {
        initSqlToJavaMappings();
        initJavaToSqlMappings();
        initSqlTypeDefaultLen();
        initSqlInfoMappings();
    }

    private static void initSqlInfoMappings() {
        sql_info_mappings.put(BaseFiledSqlInfo.class, (sqlInfoClass) -> new MysqlFiledSqlInfo());
        sql_info_mappings.put(BaseKeySqlInfo.class, (sqlInfoClass) -> new MysqlKeySqlInfo());
        sql_info_mappings.put(BaseTableSqlInfo.class, (sqlInfoClass) -> new MysqlTableSqlInfo());
        sql_info_mappings.put(BaseDataBaseSqlInfo.class, (sqlInfoClass) -> new MysqlDataBaseSqlInfo());
    }

    private static void initSqlToJavaMappings() {
        sql_to_java_mappings.put(MysqlFiledTypeConstants.VARCHAR, String.class);
        sql_to_java_mappings.put(MysqlFiledTypeConstants.CHAR, String.class);
        sql_to_java_mappings.put(MysqlFiledTypeConstants.TINY_INT, Integer.class);
        sql_to_java_mappings.put(MysqlFiledTypeConstants.SMALL_INT, Integer.class);
        sql_to_java_mappings.put(MysqlFiledTypeConstants.MEDIUM_INT, Integer.class);
        sql_to_java_mappings.put(MysqlFiledTypeConstants.INT, Integer.class);
        sql_to_java_mappings.put(MysqlFiledTypeConstants.BIG_INT, Long.class);
        sql_to_java_mappings.put(MysqlFiledTypeConstants.DATA_TIME, LocalDateTime.class);
        sql_to_java_mappings.put(MysqlFiledTypeConstants.TIME_STAMP,Timestamp.class);
        sql_to_java_mappings.put(MysqlFiledTypeConstants.DECIMAL,BigDecimal.class);
        sql_to_java_mappings.put(MysqlFiledTypeConstants.FLOAT,Float.class);
        sql_to_java_mappings.put(MysqlFiledTypeConstants.DOUBLE,Double.class);
    }

    private static void initJavaToSqlMappings() {
        java_to_sql_mappings.put(String.class, MysqlFiledTypeConstants.VARCHAR);
        java_to_sql_mappings.put(Integer.class, MysqlFiledTypeConstants.INT);
        java_to_sql_mappings.put(Byte.class,MysqlFiledTypeConstants.SMALL_INT);
        java_to_sql_mappings.put(Short.class, MysqlFiledTypeConstants.MEDIUM_INT);
        java_to_sql_mappings.put(Long.class, MysqlFiledTypeConstants.BIG_INT);
        java_to_sql_mappings.put(long.class, MysqlFiledTypeConstants.BIG_INT);
        java_to_sql_mappings.put(BigDecimal.class, MysqlFiledTypeConstants.DECIMAL);
        java_to_sql_mappings.put(Float.class, MysqlFiledTypeConstants.FLOAT);
        java_to_sql_mappings.put(Double.class, MysqlFiledTypeConstants.DOUBLE);
        java_to_sql_mappings.put(LocalDateTime.class, MysqlFiledTypeConstants.DATA_TIME);
        java_to_sql_mappings.put(Date.class, MysqlFiledTypeConstants.DATA_TIME);
        java_to_sql_mappings.put(Timestamp.class, MysqlFiledTypeConstants.TIME_STAMP);
    }

    private static void initSqlTypeDefaultLen() {
        default_len.put(MysqlFiledTypeConstants.VARCHAR, "255");
        default_len.put(MysqlFiledTypeConstants.CHAR, "255");
        //对于decimal类型,最大位数为13,小数点位数为3
        default_len.put(MysqlFiledTypeConstants.DECIMAL, "13-3");
    }
}
